package com.elinkway.infinitemovies.utils;

import com.elinkway.infinitemovies.application.MoviesApplication;
import com.elinkway.infinitemovies.download.CDNDownloadHandler;
import com.elinkway.infinitemovies.download.DownloadJob;
import com.leeco.pp.entity.CvcNormalTask;
import com.leeco.pp.entity.CvcTask;
import com.leeco.pp.func.CvcHelper;
import com.leeco.pp.listener.CvcTaskListener;
import com.leeco.pp.listener.OnStartStatusChangeListener;

/**
 * Created by huangxiaojian on 2017/7/4.
 *
 * 云盘资源下载类
 */

public class CvcHelperManager {

    public final static String TAG = "CvcHelperManager";
    private DownloadJob mCurrentDownloadJob;

    private static CvcHelper mCvcHelper;

    private volatile static CvcHelperManager mCvcHelperManager;

    private CvcHelperManager() {

    }

    public static CvcHelperManager getInstance() {
        if (mCvcHelperManager == null) {
            synchronized (CvcHelperManager.class) {
                if (mCvcHelperManager == null) {
                    mCvcHelperManager = new CvcHelperManager();
                }
            }
        }
        if (mCvcHelper == null) {
            mCvcHelper = CvcHelper.getInstance(MoviesApplication.getInstance(), getStartParams(), true);

            mCvcHelper.setOnStartStatusChangeListener(new OnStartStatusChangeListener() {

                @Override
                public void onLinkShellStartComplete(int statusCode) {

                }

                @Override
                public void onCvcStartComplete(int statusCode) {
                    if(statusCode == 0) {
                        // 启动成功
                        LogUtils.e(TAG, "-------CVC START SUCCESS");
                    }
                }

                @Override
                public void onCvcServiceDisconnected() {

                }
            });
            mCvcHelper.setTaskListener(new CvcTaskListener() {
                @Override
                public void onTaskStateChanged(CvcTask cvcTask, CvcTask.TaskState taskState, CvcTask.TaskState taskState1) {
                    if (taskState1 == CvcTask.TaskState.SUCCESS || taskState1 == CvcTask.TaskState.FAILED
                            || taskState1 == CvcTask.TaskState.PAUSED) {
                        LogUtils.e(TAG, "current cvctask's state" + taskState1);
                        CDNDownloadHandler.isFinalState = true;
                    }
                }

                @Override
                public void onDownloadProgressUpdate(CvcTask cvcTask, long downloadedBytes, long totalBytes) {
                    LogUtils.e(TAG, "downloadedBytes is" + downloadedBytes + ",,,,,,,,,,,totalBytes is" + totalBytes);
                    mCvcHelperManager.updateDownloadState((CvcNormalTask) cvcTask, downloadedBytes, totalBytes);
                }
            });
        }
        return mCvcHelperManager;
    }

    /**
     * 配置CVC启动参数
     *
     * @return
     */
    private static String getStartParams() {
        //appId，每个应用单独分配
        String cvcLogDir = "/sdcard/cvc/log"; // CVC日志目录
        String cvcFileDir = "/sdcard/cvc/files";
        return "app_id=" + IdUtils.CVC_APP_ID + "&port=" + IdUtils.CVC_APP_PORT + "&log_dir=" + cvcLogDir + "&storage_path" + cvcFileDir; // 以key=value的形式返回启动参数，多个启动参数之间以&连接，其它启动参数参见附录
    }

    public void startCvcHelperService() {
        if (mCvcHelper != null) {
            // 启动CVC
            mCvcHelper.start(true);
        }
    }

    public void startDownload(CvcTask task) {
        if (mCvcHelper != null && task != null) {
            // 开始下载
            mCvcHelper.commitTask(task);
        }
    }

    public void pauseDownload(CvcTask task) {
        if (mCvcHelper != null && task != null) {
            // 暂停下载
            mCvcHelper.pauseTask(task.getId());
        }
    }

    public void resumeDownload(CvcTask task) {
        if (mCvcHelper != null && task != null) {
            // 恢复下载
            mCvcHelper.resumeTask(task.getId());
        }
    }

    public void deleteDownload(CvcTask task) {
        if (mCvcHelper != null && task != null) {
            // 删除下载任务
            mCvcHelper.deleteTask(task.getId(), true);
        }
    }

    public void setDownloadJob(DownloadJob downloadJob) {
        mCurrentDownloadJob = downloadJob;
    }

    public void setTaskListener(CvcTaskListener cvcTaskListener) {
        if (mCvcHelper == null) {
            return;
        }
        mCvcHelper.setTaskListener(cvcTaskListener);
    }

    private void updateDownloadState(CvcNormalTask cvcTask, long downloadedBytes, long totalBytes) {
        if (mCurrentDownloadJob != null) {
            mCurrentDownloadJob.setDownloadedSize(downloadedBytes);
            mCurrentDownloadJob.setRate();
            LogUtils.e(TAG, "cvctask's state" + cvcTask.getState() + ",,,,,,,,,,,cvctask's download byte" + cvcTask.getDownloadBytes());
        }
    }
}
